<template>
  <div class="cs-page-container">
    <div class="cs-filter-container">
      <el-input
        v-model.trim="listQuery.targetUserId"
        clearable
        placeholder="请输入用户ID"
      />
      <!-- <el-input
        v-model="listQuery.nickname"
        clearable
        placeholder="用户昵称"
        style="width:130px"
      /> -->
      <el-select
        v-model="listQuery.state"
        clearable
        filterable
        placeholder="状态"
        style="width:100px"
      >
        <el-option
          v-for="(v, name) in stateMap"
          :key="name"
          :label="v"
          :value="name"
        />
      </el-select>
      <el-date-picker
        v-model="daterangeValue"
        :picker-options="daterangePickerOptions"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="创建起始时间"
        end-placeholder="创建结束时间"
        value-format="yyyy/MM/dd"
        style="width:250px"
        @change="daterangeChangedTimestamp"
      />
      <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        type="info"
        size="small"
        icon="el-icon-refresh-right"
        @click="resetFilter"
      >重置</el-button>
      <el-button type="primary" @click="createDialogVisible = true">创建消息</el-button>
      <el-button type="warning" @click="onHandleAll(2)">批量发送</el-button>
      <el-button type="danger" @click="onHandleAll(1)">批量删除</el-button>
    </div>

    <div class="cs-filter-result">
      <el-table
        v-loading="listLoading"
        :data="list"
        :height="'calc(100vh - 250px)'"
        border
        fit
        size="small"
        highlight-current-row
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column align="center" label="用户ID" prop="targetUserId">
          <template slot-scope="{ row }">
            <TooptipsCom :user-id="row.targetUserId" />
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="用户昵称" prop="nickname" /> -->
        <el-table-column align="center" label="创建时间" width="150">
          <template slot-scope="{ row }">{{ row.createTime | parseTime }}</template>
        </el-table-column>
        <el-table-column align="center" label="推送消息内容" prop="pushContent" />
        <el-table-column align="center" label="系统消息内容" prop="content" />
        <el-table-column align="center" label="备注" prop="remark" />
        <el-table-column align="center" label="状态">
          <template slot-scope="{ row }">{{ stateMap[row.state] }}</template>
          <!-- <template slot-scope="{ row }">{{ filterState(row.state) }}</template> -->
        </el-table-column>
        <el-table-column align="center" label="发送时间" width="150">
          <template slot-scope="{ row }">{{ row.updateTime | parseTime }}</template>
        </el-table-column>
        <!-- <el-table-column align="center" label="操作" fixed="right" width="160">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              size="mini"
              @click="onUnForbid(row)"
            >解封</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <TablePagination
        :current-page="listQuery.page"
        :limit="listQuery.limit"
        :total="total"
        :loading="listLoading"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChangeOffset"
      />
    </div>
    <DialogCreateMsg
      :visible.sync="createDialogVisible"
      :user-ids="userids"
      @success="createMsg"
    />
  </div>
</template>

<script>
  import TablePagination from '@/components/TablePagination'
  import DialogCreateMsg from './components/DialogCreateMsg'

  import { xhrListSysMsg, xhrCreateSysMsg, xhrDelSysMsg, xhrPushSysMsg } from '@api/account'
  export default {
    name: 'SysMsg',
    components: { TablePagination, DialogCreateMsg },
    data() {
      return {
        list: null,
        total: 0,
        userids: '',
        selectRows: [],
        daterangeValue: null,
        listQuery: {
          page: 1,
          offset: 0,
          targetUserId: null,
          state: null,
          limit: 10
        },
        createDialogVisible: false,
        stateMap: {
          '-1': '用户id不正确',
          0: '待发送',
          1: '发送中',
          2: '发送成功',
          3: '发送失败'
        }
      }
    },
    created() {
      this.doSearch()
    },
    methods: {
      handleSelectionChange(rows) {
        this.selectRows = rows
      },
      doSearch() {
        this.listLoading = true
        xhrListSysMsg(this.listQuery)
          .then(resp => {
            this.list = resp.data.list
            this.total = resp.data.count
            this.listLoading = false
          })
          .catch(err => {
            this.$message.error(err)
            this.listLoading = false
          })
      },
      filterState(state) {
        const obj = {
          '-1': '用户id不正确',
          0: '待发送',
          1: '发送中',
          2: '发送成功',
          3: '发送失败'
        }
        return obj[state]
      },
      createMsg(formData) {
        xhrCreateSysMsg(formData)
          .then(resp => {
            this.$message.success('生成成功')
            this.createDialogVisible = false
            this.doSearch()
          })
          .catch(err => {
            this.$message.error(err)
            this.listLoading = false
          })
      },
      onHandleAll(status) {
        if (!this.selectRows.length) {
          this.$message.warning('请选择一条数据')
        } else {
          const ids = this.selectRows.map(item => item.id)
          if (status === 1) {
            this.$confirm('确定删除？').then(() => {
              xhrDelSysMsg(ids).then(() => {
                this.$message.success('操作成功')
                this.doSearch()
              })
            })
          } else if (status === 2) {
            xhrPushSysMsg(ids).then(() => {
              this.$message.success('操作成功')
              this.doSearch()
            })
          }
        }
      }
    }
  }
</script>
